package a66weding.com.jiehuntong.pager;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.common.utils.CUtils;

/**
 * Created by admin on 2016/6/2.
 */
public class Tools_CreatestoryPager extends BasePager implements View.OnClickListener {

    int num = 500;//限制的最大字数

    private EditText addpingNun_et;
    private CharSequence temp;
    private int selectionStart, selectionEnd;
    private TextView tvLeft, tvCenter, tvRight, pinglunNum_tv;
    private Dialog dialog;
    private Button saveBtn, quxiaoBtn;

    public Tools_CreatestoryPager(Activity activity) {
        super(activity);
    }

    @Override
    protected View getView() {
        View myBodyView = View.inflate(mActivity, R.layout.activity_tools_createstory, null);
        addpingNun_et = (EditText) myBodyView.findViewById(R.id.addpinglun_et);
        pinglunNum_tv = (TextView) myBodyView.findViewById(R.id.pinglunnum_tv);

        tvLeft = (TextView) myBodyView.findViewById(R.id.headleft_title_tv);
        tvCenter = (TextView) myBodyView.findViewById(R.id.headcenter_title_tv);
        tvRight = (TextView) myBodyView.findViewById(R.id.headright_title_tv);
        tvLeft.setText("创建故事");
        tvCenter.setText("编辑故事");
        tvRight.setText("保存");
        tvRight.setOnClickListener(this);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });

        /**
         *为EditText文本框添加监听
         */
        addpingNun_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = num - s.length();
                pinglunNum_tv.setText("" + number);
                selectionStart = addpingNun_et.getSelectionStart();
                selectionEnd = addpingNun_et.getSelectionEnd();
                if (temp.length() > num) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionStart;
                    addpingNun_et.setText(s);
                    addpingNun_et.setSelection(tempSelection);//设置光标在最后
                    Toast.makeText(mActivity, "不能超过500字", Toast.LENGTH_SHORT).show();
                }
            }

        });
        return myBodyView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headright_title_tv:
                showDialog();
                break;
        }
    }

    private void showDialog() {
        dialog = new Dialog(mActivity, R.style.transparentFrameWindowStyle);
        View view = View.inflate(mActivity, R.layout.tools_dialog_save, null);
        saveBtn = (Button) view.findViewById(R.id.dialog_save_button);
        quxiaoBtn = (Button) view.findViewById(R.id.dialog_quxiao_button);
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        quxiaoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.height = lp.MATCH_PARENT;   //高度设置
        lp.width = lp.MATCH_PARENT;    //宽度设置
        dialogWindow.setGravity(Gravity.CENTER | Gravity.CENTER);
        dialogWindow.setAttributes(lp);
        dialog.show();
    }
}
